export function compose (...funcs) {
  if (funcs.length === 0) {
    return arg => arg
  }
  if (funcs.length === 1) {
    return funcs[0]
  }
  return funcs.reduce((a, b) => (...args) => a(b(...args)))
}

export const curry = (fn, ...args) =>
  fn.length <= args.length
    ? fn(...args)
    : curry.bind(null, fn, ...args)

export function columnLabel (label) {
  return { label }
}

export function isEmpty (value) {
  return value || value === 0 ? value : '--'
}

export function formatAt (moment, value) {
  return value === '--' ? value : moment(value).format('YYYY-MM-DD HH:mm:ss')
}

export function formatArr (moment, value) {
  return value === '--' ? value : [moment(value).format('YYYY-MM-DD'), moment(value).format('HH:mm:ss')]
}

export function memoize (f) {
  let cache = {}
  return function () {
    let arg_str = JSON.stringify(arguments)
    cache[arg_str] = cache[arg_str] || f.apply(f, arguments)
    return cache[arg_str]
  }
};

export function getlabel (data, value) {
  return data.filter(v => v.value === value)[0].label
}

export function isArrLength (arr) {
  return arr.length === 0 ? '--' : arr
}

export const dutime = function (startTime, endTime) {
  let m1 = this.$moment(startTime)
  let m2 = this.$moment(endTime)
  let du = this.$moment.duration(m1 - m2, 'ms')
  let day = Math.abs(du.get('days'))
  let hours = Math.abs(du.get('hours'))
  let mins = Math.abs(du.get('minutes'))
  let ss = Math.abs(du.get('seconds'))
  if (day > 0) return day + '天' + hours + '小时' + mins + '分' + ss + '秒'
  if (hours > 0) return hours + '小时' + mins + '分' + ss + '秒'
  if (mins > 0) return mins + '分' + ss + '秒'
  if (ss >= 0) return ss + '秒'
}

export const diff = function (stra, strb) {
  let a = stra.slice(1)
  let b = strb.slice(1)
  return a < b ? -1 : a > b ? 1 : 0
}

export function isNull (value) {
  return value === null
}

export const isInstance = (function () {
  let map = {}
  return function (key, instance) {
    if (key in map) {
      return map[key]
    } else {
      let ret = map[key] = instance
      document.body.appendChild(instance.$el)
      return ret
    }
  }
}())
